using UnityEngine;
using System.Collections;

//using System.Data.SQLite;
using Mono.Data.SqliteClient;



public class SQL : MonoBehaviour
{
	public static SQL SP;

	protected SqliteConnection mDBCon;
	//SqliteCommand cmd;

	public string msNombre;
	public int miLevelMax;
	public int miLevelActual;
	public string msDispositivo = "default";
	private string msFile = "Data.db";
	private string msTabla = "ARKANOID";

	void AbrirDB()
	{
		if (Application.platform == RuntimePlatform.IPhonePlayer)
		{
			msDispositivo = "IPhone";
			mDBCon = new SqliteConnection("URI=file:" + Application.dataPath + "/../" + msFile);
		}
		if (Application.platform == RuntimePlatform.Android)
		{
			//dbconn = new SqliteConnection("URI=file:" + Application.dataPath + "/assets/Data.db");
			msDispositivo = "Android";
			mDBCon = new SqliteConnection("URI=file:" + Application.persistentDataPath + "/" + msFile); //Funciona en Android
		}
		else
		{
			GUI.Label(new Rect(4, 70, 400, 400), Application.dataPath + "/" + msFile);
			msDispositivo = "PC";
			mDBCon = new SqliteConnection("URI=file:" + Application.dataPath + "/" + msFile);  //Funciona dentro de UNity
            //mDBCon = new SqliteConnection("Data Source=" + Application.dataPath + "/" + msFile);
            //mDBCon = new SqliteConnection("URI=file:db\\" + Application.dataPath + "\\" + msFile);
            //mDBCon = new SqliteConnection("Data Source=file:" + Application.dataPath + "/" + msFile);
            //mDBCon = new SqliteConnection("URI=file:" + msFile + ",version=3");  //Funciona dentro de UNity
		}

		mDBCon.Open();

	}

	void CerrarDB()
	{
		if (mDBCon != null)
		{
			mDBCon.Close();
			mDBCon = null;
		}
	}

	void Awake()
	{

		SP = this;

	
		CrearTabla(); //crea la tabla si no existe.

		AbrirDB();

		try
		{
			SqliteCommand lCmd = new SqliteCommand("SELECT Nombre, LevelMax, LevelActual FROM " + msTabla + " WHERE ID=1", mDBCon);
			SqliteDataReader lReader = lCmd.ExecuteReader();

			if (lReader.HasRows)
			{
				while (lReader.Read())
				{
					string lsNombre = lReader.GetString(0);
					int liLevelMax = lReader.GetInt32(1);
					int liLevelActual = lReader.GetInt32(2);

					msNombre = lsNombre;
					miLevelMax = liLevelMax;
					miLevelActual = liLevelActual;

					//Debug.Log(lsNombre + "---" + liLevelMax + "-----" + liLevelActual);
				}
			}
			else //creamos la primera tabla  con el level 1
			{
				miLevelMax = 1;
				miLevelActual = 1;
				CrearPrimerRegistro();
				
			}

			lReader.Close();
			lReader = null;
		}
		catch
		{
			Debug.Log("Error reading DB (Awake)");
		}


		//cmd.Dispose();
		//cmd = null;
		CerrarDB();
		//mdDBCon.Close();
		//mdDBCon = null;


	}

	void CrearTabla()
	{
		AbrirDB();


		SqliteCommand lCmd = new SqliteCommand("CREATE TABLE IF NOT EXISTS " + msTabla + " (ID, NOMBRE, LEVELMAX, LEVELACTUAL, PUNTOS)", mDBCon);
		SqliteDataReader lReader = lCmd.ExecuteReader();
		lReader.Close();
		lReader = null;
		CerrarDB();
		
	}

	public void CrearPrimerRegistro()
	{
		AbrirDB();
		SqliteCommand lCmd = new SqliteCommand("INSERT INTO " + msTabla + " VALUES (1, 'PrimerRegistro', 1, 1, -1)", mDBCon);
		SqliteDataReader lReader = lCmd.ExecuteReader();
		lReader.Close();
		lReader = null;
		miLevelActual = 1;
		miLevelMax = 1;
		CerrarDB();
	}

	public int SelectLevelMax()
	{
		AbrirDB();

		int liLevelMax = -1;
		try
		{
			SqliteCommand lCmd = new SqliteCommand("SELECT Nombre, LevelMax FROM " + msTabla + " WHERE ID=1", mDBCon);
			SqliteDataReader lReader = lCmd.ExecuteReader();
			
			if (lReader.HasRows)
			{
				while (lReader.Read())
				{
					string lsNombre = lReader.GetString(0);
					liLevelMax = lReader.GetInt32(1);
					
					msNombre = lsNombre;
					miLevelMax = liLevelMax;
				}
			}
			lReader.Close();
			lReader = null;
		}
		catch
		{
			//Debug.Log("Error reading DB (Select)");
		}
		CerrarDB();

		return liLevelMax;
	}

	public int SelectLevelActual()
	{
		AbrirDB();

		int liLevelActual = -1;
		try
		{
			SqliteCommand lCmd = new SqliteCommand("SELECT Nombre, LevelActual FROM " + msTabla + " WHERE ID=1", mDBCon);
			SqliteDataReader lReader = lCmd.ExecuteReader();

			if (lReader.HasRows)
			{
				while (lReader.Read())
				{
					string lsNombre = lReader.GetString(0);
					liLevelActual = lReader.GetInt32(1);

					msNombre = lsNombre;
					miLevelActual = liLevelActual;
				}
			}
			lReader.Close();
			lReader = null;
		}
		catch
		{
			Debug.Log("Error reading DB (Select)");
		}
		CerrarDB();

		return liLevelActual;
	}





	public void UpdateLevelMax(int liLevelMax)
	{
		AbrirDB();
		SqliteCommand lCmd = new SqliteCommand("UPDATE " + msTabla + " SET LevelMax=" + liLevelMax + ", Nombre='LM' WHERE ID=1", mDBCon);
		SqliteDataReader lReader = lCmd.ExecuteReader();
		lReader.Close();
		lReader = null;
		miLevelMax = liLevelMax;
		CerrarDB();
	}

	public void UpdateLevelActual(int liLevelActual)
	{
		AbrirDB();
		SqliteCommand lCmd = new SqliteCommand("UPDATE " + msTabla + " SET LevelActual=" + liLevelActual + ", Nombre='LA' WHERE ID=1", mDBCon);
		SqliteDataReader lReader = lCmd.ExecuteReader();
		lReader.Close();
		lReader = null;
		miLevelActual = liLevelActual;
		CerrarDB();
	}


/*
	public void InsertarTablaLevelActual(int liLevelActual)
	{
		AbrirDB();
		SqliteCommand lCmd = new SqliteCommand("INSERT INTO " + msTabla + " VALUES (1, 'LA', " + miLevelMax + ", " + liLevelActual + ", -1)", mDBCon);
		SqliteDataReader lReader = lCmd.ExecuteReader();
		lReader.Close();
		lReader = null;
		miLevelActual = liLevelActual;
		CerrarDB();
	}

	public void InsertarTablaLevelMax(int liLevelMax)
	{
		AbrirDB();
		SqliteCommand lCmd = new SqliteCommand("INSERT INTO " + msTabla + " VALUES (1, 'LM', " + liLevelMax + ", " + liLevelMax + ", -1)", mDBCon);
		SqliteDataReader lReader = lCmd.ExecuteReader();
		lReader.Close();
		lReader = null;
		miLevelMax = liLevelMax;
		CerrarDB();
	}
*/

}
